package com.qifenqian.turbot.module.ui.loginetc

import android.content.Context
import android.content.Intent
import com.bumptech.glide.Glide
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.config.Constant.TAG_ACTIVITY_COLLECTOR_REGISTER
import com.qifenqian.turbot.module.contract.AccountContract
import com.qifenqian.turbot.module.dialog.ExampleImageDialog
import com.qifenqian.turbot.module.dialog.TYPE_EXAMPLE_BUSINESS_LICENCE
import com.qifenqian.turbot.module.dialog.TYPE_EXAMPLE_RESTAURANT_PHOTO
import com.qifenqian.turbot.module.dialog.TYPE_EXAMPLE_SERVICE_LICENCE
import com.qifenqian.turbot.module.entity.response.UploadEntity
import com.qifenqian.turbot.module.presenter.AccountPresenter
import com.qifenqian.turbot.util.ActivityCollector
import com.qifenqian.turbot.util.MyGlideEngine
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.internal.entity.CaptureStrategy
import kotlinx.android.synthetic.main.activity_identity_verify_photo.*
import kotlinx.android.synthetic.main.layout_complete_bar.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2018/12/17 14:44
 * @description 资质认证第二个界面
 * @see
 * @since
 */
private const val REQUEST_IMAGE_CHOOSE_LICENSE = 0x311
private const val REQUEST_IMAGE_CHOOSE_PHOTO1 = 0x312
private const val REQUEST_IMAGE_CHOOSE_PHOTO2 = 0x313
private const val REQUEST_IMAGE_CHOOSE_PHOTO3 = 0x314
private const val REQUEST_IMAGE_CHOOSE_PHOTO4 = 0x315
private const val REQUEST_IMAGE_CHOOSE_SERVICE_LICENSE = 0x316

class IdentityVerifyPhotoActivity : BaseActivity<AccountPresenter, AccountContract.View>(), AccountContract.View {

    companion object {
        fun startThis(context: Context, userCode: String) {
            val intent = Intent(context, IdentityVerifyPhotoActivity::class.java)
            intent.putExtra("userCode", userCode)
            context.startActivity(intent)
        }
    }

    private var mUserCode: String = ""

    override fun initial() {
        ActivityCollector.addActivity(this, TAG_ACTIVITY_COLLECTOR_REGISTER)
        mUserCode = intent.getStringExtra("userCode")
        iv_complete_bar_1.setImageResource(R.mipmap.ic_verify_ok)
        iv_complete_bar_2.setImageResource(R.mipmap.ic_verify_default)
        iv_complete_bar_3.setImageResource(R.drawable.shape_circle)
        btn_identity_verify_photo_example1.setOnClickListener {
            // TODO: 2018/12/25 14:39 查看营业执照示例 ----->
            ExampleImageDialog.newInstance(TYPE_EXAMPLE_BUSINESS_LICENCE).show(this, "")
        }
        btn_identity_verify_photo_example2.setOnClickListener {
            // TODO: 2018/12/25 14:39 查看店铺照片示例 ----->
            ExampleImageDialog.newInstance(TYPE_EXAMPLE_RESTAURANT_PHOTO).show(this, "")
        }
        btn_identity_verify_photo_example3.setOnClickListener {
            // TODO: 2018/12/25 14:39 查看餐饮许可示例 ----->
            ExampleImageDialog.newInstance(TYPE_EXAMPLE_SERVICE_LICENCE).show(this, "")
        }
        iv_identity_verify_photo_license.setOnClickListener {
            chooseImage(REQUEST_IMAGE_CHOOSE_LICENSE)
        }
        iv_identity_verify_photo1.setOnClickListener {
            chooseImage(REQUEST_IMAGE_CHOOSE_PHOTO1)
        }
        iv_identity_verify_photo2.setOnClickListener {
            chooseImage(REQUEST_IMAGE_CHOOSE_PHOTO2)
        }
        iv_identity_verify_photo3.setOnClickListener {
            chooseImage(REQUEST_IMAGE_CHOOSE_PHOTO3)
        }
        iv_identity_verify_photo3.setOnClickListener {
            chooseImage(REQUEST_IMAGE_CHOOSE_PHOTO3)
        }
        iv_identity_verify_photo_service_license.setOnClickListener {
            chooseImage(REQUEST_IMAGE_CHOOSE_SERVICE_LICENSE)
        }
        btn_identity_verify_photo_next.setOnClickListener {
            if (iv_identity_verify_photo_license.tag != "ok") {
                toast("请上传营业执照")
                return@setOnClickListener
            }
            if (iv_identity_verify_photo1.tag != "ok") {
                toast("请上传店铺照片")
                return@setOnClickListener
            }
            if (iv_identity_verify_photo2.tag != "ok") {
                toast("请上传店铺照片")
                return@setOnClickListener
            }
            if (iv_identity_verify_photo3.tag != "ok") {
                toast("请上传店铺照片")
                return@setOnClickListener
            }
//            if (iv_identity_verify_photo4.tag != "ok") {
//                toast("请上传")
//                return@setOnClickListener
//            }
            if (iv_identity_verify_photo_service_license.tag != "ok") {
                toast("请上传餐饮服务许可证")
                return@setOnClickListener
            }
            mPhotoList.add(mPhoto1Url)
            mPhotoList.add(mPhoto2Url)
            mPhotoList.add(mPhoto3Url)
//            mPhotoList.add(mPhoto4Url!!)
            mPresenter!!.identityVerifySecond(mUserCode, mLicenseUrl, mPhotoList, mServiceLicenseUrl)
            // TODO: 2018/12/25 15:39 移除 ----->
            DialogUploadSuccessActivity.startThis(this)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this, TAG_ACTIVITY_COLLECTOR_REGISTER)
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_identity_verify_photo
    }

    override fun bindPresenter(): AccountPresenter? {
        return AccountPresenter(this, this)
    }

    private fun chooseImage(requestCode: Int) {
        Matisse.from(this@IdentityVerifyPhotoActivity)
                .choose(MimeType.ofImage())
                .countable(false)
                .capture(true)
                .captureStrategy(CaptureStrategy(true, Constant.AUTHORITY))
                .maxSelectable(1)
                .originalEnable(true)
                .maxOriginalSize(10)
                .imageEngine(MyGlideEngine())
                .forResult(requestCode)
    }

    private var mPhotoList: MutableList<String> = mutableListOf()

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                REQUEST_IMAGE_CHOOSE_LICENSE         -> mPresenter!!.uploadFile(REQUEST_IMAGE_CHOOSE_LICENSE, Matisse.obtainPathResult(data)[0])
                REQUEST_IMAGE_CHOOSE_PHOTO1          -> mPresenter!!.uploadFile(REQUEST_IMAGE_CHOOSE_PHOTO1, Matisse.obtainPathResult(data)[0])
                REQUEST_IMAGE_CHOOSE_PHOTO2          -> mPresenter!!.uploadFile(REQUEST_IMAGE_CHOOSE_PHOTO2, Matisse.obtainPathResult(data)[0])
                REQUEST_IMAGE_CHOOSE_PHOTO3          -> mPresenter!!.uploadFile(REQUEST_IMAGE_CHOOSE_PHOTO3, Matisse.obtainPathResult(data)[0])
//                REQUEST_IMAGE_CHOOSE_PHOTO4          -> mPresenter!!.uploadFile(REQUEST_IMAGE_CHOOSE_PHOTO4, Matisse.obtainPathResult(data)[0])
                REQUEST_IMAGE_CHOOSE_SERVICE_LICENSE -> mPresenter!!.uploadFile(REQUEST_IMAGE_CHOOSE_SERVICE_LICENSE, Matisse.obtainPathResult(data)[0])
            }
        }
    }

    private var mLicenseUrl: String = ""
    private var mPhoto1Url: String = ""
    private var mPhoto2Url: String = ""
    private var mPhoto3Url: String = ""
    //    private var mPhoto4Url: String = ""
    private var mServiceLicenseUrl: String = ""

    override fun uploadFileResult(isSuccess: Boolean, code: Int, msg: String?, requestCode: Int, uploadEntity: UploadEntity?) {
        if (isSuccess) {
            when (requestCode) {
                REQUEST_IMAGE_CHOOSE_LICENSE         -> {
                    mLicenseUrl = uploadEntity!!.url
                    Glide.with(this)
                            .load(mLicenseUrl)
                            .into(iv_identity_verify_photo_license)
                    iv_identity_verify_photo_license.tag = "ok"
                }
                REQUEST_IMAGE_CHOOSE_PHOTO1          -> {
                    mPhoto1Url = uploadEntity!!.url
                    Glide.with(this)
                            .load(mPhoto1Url)
                            .into(iv_identity_verify_photo1)
                    iv_identity_verify_photo1.tag = "ok"
                }
                REQUEST_IMAGE_CHOOSE_PHOTO2          -> {
                    mPhoto2Url = uploadEntity!!.url
                    Glide.with(this)
                            .load(mPhoto2Url)
                            .into(iv_identity_verify_photo2)
                    iv_identity_verify_photo2.tag = "ok"
                }
                REQUEST_IMAGE_CHOOSE_PHOTO3          -> {
                    mPhoto3Url = uploadEntity!!.url
                    Glide.with(this)
                            .load(mPhoto3Url)
                            .into(iv_identity_verify_photo3)
                    iv_identity_verify_photo3.tag = "ok"
                }
//                REQUEST_IMAGE_CHOOSE_PHOTO4          -> {
//                    mPhoto4Url = uploadEntity!!.url
//                    Glide.with(this)
//                            .load(mPhoto4Url)
//                            .into(iv_identity_verify_photo4)
//                    iv_identity_verify_photo4.tag = "ok"
//                }
                REQUEST_IMAGE_CHOOSE_SERVICE_LICENSE -> {
                    mServiceLicenseUrl = uploadEntity!!.url
                    Glide.with(this)
                            .load(mServiceLicenseUrl)
                            .into(iv_identity_verify_photo_service_license)
                    iv_identity_verify_photo_service_license.tag = "ok"
                }
            }
        }
        toast(msg!!)
    }

    override fun identityVerifySecondResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            DialogUploadSuccessActivity.startThis(this)
        } else {
            toast(msg!!)
        }
    }
}
